function ajax(option) {
  if(option.jsonp){
    var cbStr = 'var '+option.jsonp+'='+option.success+'';
    var url = option.url+'?cb='+option.jsonp ;
    function createJsonp(url) {
      var scriptEle = document.createElement('script');
      scriptEle.src = url;
      document.body.appendChild(scriptEle);
    }
    var preScript = document.createElement('script');
    preScript.innerHTML = cbStr ;
    document.body.appendChild(preScript);
    createJsonp(url);
    return;
  }
  var statusError = {
    404:"not found!",
    500:"server error"
  };
  function serialize(data) {
    var parmas = '';
    for(var prop in data){
      parmas += prop+"=" + data[prop] + "&";
    }
    return parmas ;
  }
  var xhr = new XMLHttpRequest()||console.error("XMLHttpRequest is not a function");
  if(option.method.toLowerCase() === 'get'){
    option.url = option.url+ '?' + (option.data ? serialize(option.data) : "");
  }
  xhr.open(option.method, option.url );
  xhr.onreadystatechange = function () {
    if(xhr.readyState === 4 && xhr.status === 200){
      option.success.call(xhr,option.dataType === 'json' ? JSON.parse(xhr.responseText) : xhr.responseText);
    }else{
      xhr.readyState === 4 && option.fail.call(xhr, statusError[xhr.status]);
    }
  };
  if(option.method.toLowerCase() === 'post'){
    if(option.contentType === 'json'){
      xhr.setRequestHeader("Content-Type", "application/json");
      xhr.send(JSON.stringify(option.data))
    }else{
      xhr.setRequestHeader("Content-Type", "application/x-www-form-encoeded;charset=utf-8");
      xhr.send(serialize(option.data))
    }
  }else{
    xhr.send(null);
  }
}

function upload(option){
  var xhr = new XMLHttpRequest();
  xhr.open('post', option.url);
  xhr.onreadystatechange = function(){
    if(xhr.readyState === 4 && xhr.status === 200){
      option.success.call(this, xhr.responseText);
    }else {
        xhr.readyState === 4 && option.fail.call(this, 'server error');
    }
  };
  xhr.upload.onprogress = function(event){
      event.speed = (event.loaded/1024)/((new Date().getTime() - xhr.originTime)/1000);
      event.process = event.loaded/event.total;
      option.process.call(xhr, event);
  };
  // xhr.setRequestHeader('Content-Type', 'multipart/form-data');
  xhr.send(option.formData);
//函数执行时保存当前时间
  xhr.originTime = new Date().getTime();
}


